﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_INV_PROD_INVENTORY
// * CreateDate    : 2014/3/26 10:56:30
// * Creater       : CodeSmith
// * LastChangeDate: 2014/3/26 10:56:30
// * LastChanger   : 2014/3/26 10:56:30
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Prod;

namespace ESOL.Business.Inv
{
    /// <summary>
    /// InvProdInventory
    /// </summary>
    public partial class InvProdInventoryBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objInvProdInventoryEntity">Entity</param>
        /// <returns>Entity</returns>
        public static InvProdInventoryEntity GetInvProdInventory(InvProdInventoryEntity objInvProdInventoryEntity)
        {
            return DataAccess.SelectSingle<InvProdInventoryEntity>(objInvProdInventoryEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objInvProdInventoryEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertInvProdInventory(InvProdInventoryEntity objInvProdInventoryEntity)
        {
            return DataAccess.Insert(objInvProdInventoryEntity);
        }

        public static bool InsertInvProdInventory(List<InvProdInventoryEntity> objInvProdInventoryEntityList)
        {
            return DataAccess.Insert(objInvProdInventoryEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objInvProdInventoryEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateInvProdInventory(InvProdInventoryEntity objInvProdInventoryEntity)
        {
            return DataAccess.Update(objInvProdInventoryEntity);
        }

        public static bool UpdateInvProdInventory(List<InvProdInventoryEntity> objInvProdInventoryEntityList)
        {
            return DataAccess.Update<InvProdInventoryEntity>(objInvProdInventoryEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objInvProdInventoryEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteInvProdInventory(InvProdInventoryEntity objInvProdInventoryEntity)
        {
            return DataAccess.Delete(objInvProdInventoryEntity);
        }

        public static bool DeleteInvProdInventory(List<InvProdInventoryEntity> objInvProdInventoryEntityList)
        {
            return DataAccess.Delete<InvProdInventoryEntity>(objInvProdInventoryEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetInvProdInventoryAll()
        {
            return DataAccess.SelectAllDataSet<InvProdInventoryEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<InvProdInventoryEntity> GetInvProdInventoryAllList()
        {
            return DataAccess.SelectAll<InvProdInventoryEntity>();
        }
        #endregion




        public static InvProdInventoryEntity AddProdInventory(InvProdInventoryEntity prodInventory, List<InvProdInventoryItemEntity> invItems)
        {
            if (prodInventory == null || invItems == null || invItems.Count == 0) return null;

            //产生入库申请单号
            if (string.IsNullOrEmpty(prodInventory.InvNo)) prodInventory.InvNo = MstCodeRuleBs.GetCurrentOrderCode("WO_INV");
            prodInventory.UpdatedDate = DateTime.Now;
            prodInventory.CreatedDate = DateTime.Now;
            List<ProdWipCurEntity> deleteWip = new List<ProdWipCurEntity>();
            List<ProdWipCurEntity> updateWip = new List<ProdWipCurEntity>();
            List<ProdWipCurEntity> insWip = new List<ProdWipCurEntity>();
            List<ProdWipHisEntity> insertWipInventoryHis = new List<ProdWipHisEntity>();
            List<InvIappiEntity> iappis = new List<InvIappiEntity>();
            InvIappEntity iapp = CreateInvIappByWipInventory(prodInventory);
            foreach (InvProdInventoryItemEntity item in invItems)
            {
                //初始明细申请单号
                item.InvNo = prodInventory.InvNo;
                
                //更新在制品状态finished-->Inv
                var wip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = Common.ProdStatus.PS_FINISH.ToString() });
                if (wip == null)
                    throw (new Exception("没有找到可以入库的在制品。 wo:" + prodInventory.WoNo + ",technic:" + item.TechnicItemId+" ,WorkgroupNo:"+item.CurWorkgroupNo));

               
                //修改完工状态的在制品记录
                //if (wip.Qty > item.ProdQty)
                //{
                //    wip.Qty = wip.Qty - item.ProdQty;
                //    updateWip.Add(wip);
                //}
                //else if (wip.Qty == item.ProdQty)
                //{
                //    deleteWip.Add(wip);
                //}
                //else
                if(wip.Qty < item.ProdQty)
                    throw (new Exception("没有足够的可入库在制品！ wo:" + prodInventory.WoNo + ",technic:" + item.TechnicItemId + " ,WorkgroupNo:" + item.CurWorkgroupNo));


                //var invWip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = "INV" });
                ////修改入库状态的在制品记录
                //if (invWip != null)
                //{//已存在入库状态的记录
                //    invWip.Qty = invWip.Qty + item.ProdQty;
                //    updateWip.Add(invWip);
                //}
                //else
                //{
                    
                //     invWip = wip;
                //     invWip.Id = Guid.NewGuid().ToString();
                //     invWip.Qty = item.ProdQty;
                //     invWip.Status = "INV";
                //     insWip.Add(invWip);
                //}
             //ProdWipHisEntity wiphis= GetWipInventoryHis(wip,prodInventory.InvNo,"INV");
             //insertWipInventoryHis.Add(wiphis);
               
                //生成入库申请仓库视图 
             InvIappiEntity iappi = CreateInvIappiByWipInventory(iapp.Id, prodInventory.ProdId, item);
             iappis.Add(iappi);
            }

            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(prodInventory,broker);
                    DataAccess.Insert(invItems, broker);
                    if(deleteWip.Count>0)DataAccess.Delete(deleteWip,broker);
                    if(updateWip.Count>0)DataAccess.Update(updateWip,broker);
                    if(insWip.Count>0)DataAccess.Insert(insWip,broker);
                    if(insertWipInventoryHis.Count>0)DataAccess.Insert(insertWipInventoryHis,broker);
                    if (iapp != null) DataAccess.Insert(iapp, broker);
                    if (iappis.Count > 0) DataAccess.Insert(iappis, broker);
                    broker.Commit();
                    return prodInventory;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static ProdWipHisEntity GetWipInventoryHis(ProdWipCurEntity wip,string transNo,string evenType)
        {
           
            ProdWipHisEntity curHis = new ProdWipHisEntity();
            curHis.Id=Guid.NewGuid().ToString();
            curHis.WoNo = wip.WoNo;
            curHis.WoTaskId = "";
            curHis.CreatedBy = wip.CreatedBy;
            curHis.CreatedDate = DateTime.Now;
            curHis.CurAssetCode = wip.CurAssetCode;
            curHis.CurRouteName = wip.CurRouteName;
            curHis.CurRouteNo = wip.CurRouteNo;
            curHis.CurTechnicName = wip.CurTechnicName;
            curHis.CurTechnicNo = wip.CurTechnicNo;
            curHis.CurWorkgroupName = wip.CurWorkgroupNo;
            curHis.CurWorkgroupNo = wip.CurWorkgroupNo;
            curHis.DeftCode1 = wip.DeftCode1;
            curHis.DeftCode2 = wip.DeftCode2;
            curHis.DeftCode3 = wip.DeftCode3;
            curHis.EventTime = DateTime.Now;
            curHis.EventType = evenType;
            curHis.NxtAssetCode = wip.NxtAssetCode;
            curHis.NxtRouteNo = wip.NxtRouteNo;
            curHis.NxtTechnicNo = wip.NxtTechnicNo;
            curHis.NxtWorkgroupNo = wip.NxtWorkgroupNo;
            curHis.PreRouteNo = wip.PreRouteNo;
            curHis.PreAssetCode = wip.PreAssetCode;
            curHis.PreTechnicNo = wip.PreTechnicNo;
            curHis.PreWorkgroupNo = wip.PreWorkgroupNo;
            curHis.ProdCode = wip.ProdCode;
            curHis.ProdName = wip.ProdName;
            curHis.ProdQty = wip.Qty;
            curHis.ProdSn = wip.ProdSn;
            curHis.Status = wip.Status;
            curHis.TechnicSeq = wip.TechnicSeq;
            curHis.UserNo = wip.UserNo;
            curHis.RelatedNo=transNo;
            return curHis;
        }

        private static InvIappiEntity CreateInvIappiByWipInventory(string appId, string prodId, InvProdInventoryItemEntity item)
        {
            InvIappiEntity iappi = new InvIappiEntity();
            iappi.Id = Guid.NewGuid().ToString();
            iappi.IoAppId = appId;
            iappi.ProductId = prodId;
            iappi.Qty = item.ProdQty;
            return iappi;
           
        }

        private static InvIappEntity CreateInvIappByWipInventory(InvProdInventoryEntity wipInv)
        {
            InvIappEntity iapp = new InvIappEntity();
            iapp.Id = Guid.NewGuid().ToString();
            iapp.CreatedBy = wipInv.CreatedBy;
            iapp.CreatedDate = DateTime.Now;
            iapp.UpdatedBy = wipInv.UpdatedBy;
            iapp.UpdatedDate = DateTime.Now;
            iapp.IsActive = wipInv.IsActive;
            iapp.AppNo = MstCodeRuleBs.GetCurrentOrderCode("InvInAppOrder");
            iapp.Type = (int)Common.InvInType.工单入库;
            iapp.RelatedId = wipInv.InvNo;
            iapp.Remark = "";
            iapp.Status = Common.WfStatus.WFS10.ToString();
            return iapp;

        }


        public static InvProdInventoryEntity UpdateProdInventory(InvProdInventoryEntity prodInventory, List<InvProdInventoryItemEntity> invItems)
        {
            if (prodInventory == null || invItems == null || invItems.Count == 0) return null;

            prodInventory.UpdatedDate = DateTime.Now;
            //List<ProdWipCurEntity> deleteWip = new List<ProdWipCurEntity>();
            //List<ProdWipCurEntity> updateWip = new List<ProdWipCurEntity>();
            //List<ProdWipCurEntity> insWip = new List<ProdWipCurEntity>();
            //List<ProdWipHisEntity> insertWipInventoryHis = new List<ProdWipHisEntity>();
            List<InvIappiEntity> iappis = new List<InvIappiEntity>();
            InvIappEntity iapp = DataAccess.SelectSingle<InvIappEntity>(new InvIappEntity { RelatedId = prodInventory.InvNo });
            List<InvIappiEntity> delIAppis = DataAccess.Select<InvIappiEntity>(new InvIappiEntity { IoAppId = iapp.Id });
            List<InvProdInventoryItemEntity> delInvItem = DataAccess.Select<InvProdInventoryItemEntity>(new InvProdInventoryItemEntity { InvId = prodInventory.Id });

            ////还原inv状态的在制品信息
            //foreach (InvProdInventoryItemEntity item in delInvItem)
            //{
            //    var wip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = Common.WoStatus.WOS_Finish.ToString() });
            //    var invWip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = "INV" });
                    
            //    if (wip != null)
            //    {//存在没有入库的在制品
            //        wip.Qty = wip.Qty + item.ProdQty;
            //        updateWip.Add(wip);
            //    }
            //    else
            //    {//不存在没有入库的在制品
            //        ProdWipCurEntity insFinishedWips = invWip;
            //        insFinishedWips.Id = Guid.NewGuid().ToString();
            //        insFinishedWips.Status = Common.WoStatus.WOS_Finish.ToString();
            //        insFinishedWips.Qty = item.ProdQty;
            //        insWip.Add(insFinishedWips);
            //    }

            //    if (invWip.Qty > item.ProdQty)
            //    {
            //        invWip.Qty = invWip.Qty - item.ProdQty;
            //        updateWip.Add(invWip);
            //    }
            //    else if(invWip.Qty==item.ProdQty)
            //    {
            //        deleteWip.Add(invWip);
            //    }

            //}

            //更新新的入库记录
            foreach (InvProdInventoryItemEntity item in invItems)
            {
                //初始明细申请单号
                item.InvNo = prodInventory.InvNo;

                #region //更新在制品状态finished-->Inv
                //var wip = updateWip.Find(delegate(ProdWipCurEntity p) { return p.WoNo == prodInventory.WoNo && p.TechnicItemId == item.TechnicItemId && p.CurWorkgroupNo == item.CurWorkgroupNo && p.Status == Common.WoStatus.WOS_Finish.ToString(); });
                //if (wip != null) //最新的finish状态的wip数
                //{
                //    //修改完工状态的在制品记录
                //    if (wip.Qty > item.ProdQty)
                //    {
                //        wip.Qty = wip.Qty - item.ProdQty;
                //    }
                //    else if (wip.Qty == item.ProdQty)
                //    {
                //        updateWip.Remove(wip);
                //        deleteWip.Add(wip);
                //    }
                //    else
                //        throw (new Exception("没有足够的可入库在制品！ wo:" + prodInventory.WoNo + ",technic:" + item.TechnicItemId + " ,WorkgroupNo:" + item.CurWorkgroupNo));

                //}
                //else
                //{
                //    wip = insWip.Find(delegate(ProdWipCurEntity p) { return p.WoNo == prodInventory.WoNo && p.TechnicItemId == item.TechnicItemId && p.CurWorkgroupNo == item.CurWorkgroupNo && p.Status == Common.WoStatus.WOS_Finish.ToString(); });
                //    if (wip != null)
                //    {
                //        if (wip.Qty > item.ProdQty)
                //        {
                //            wip.Qty = wip.Qty - item.ProdQty;
                //        }
                //        else if (wip.Qty == item.ProdQty)
                //        {
                //            insWip.Remove(wip);
                //        }
                //        else
                //            throw (new Exception("没有足够的可入库在制品！ wo:" + prodInventory.WoNo + ",technic:" + item.TechnicItemId + " ,WorkgroupNo:" + item.CurWorkgroupNo));

                //    }
                //    else
                //    {//之前没有修改过这条明细记录
                //        wip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = Common.WoStatus.WOS_Finish.ToString() });
                //        if (wip == null)
                //            throw (new Exception("没有找到可以入库的在制品。 wo:" + prodInventory.WoNo + ",technic:" + item.TechnicItemId + " ,WorkgroupNo:" + item.CurWorkgroupNo));
                //        else
                //        {
                //            if (wip.Qty > item.ProdQty)
                //            {
                //                wip.Qty = wip.Qty - item.ProdQty;
                //                updateWip.Add(wip);
                //            }
                //            else if (wip.Qty == item.ProdQty)
                //            {
                //                deleteWip.Add(wip);
                //            }
                //            else
                //                throw (new Exception("没有足够的可入库在制品！ wo:" + prodInventory.WoNo + ",technic:" + item.TechnicItemId + " ,WorkgroupNo:" + item.CurWorkgroupNo));

                //        }
                //    }
                //}
                #endregion



                //var invWip = updateWip.Find(delegate(ProdWipCurEntity p) { return p.WoNo == prodInventory.WoNo && p.TechnicItemId == item.TechnicItemId && p.CurWorkgroupNo == item.CurWorkgroupNo && p.Status == "INV"; });
                //if (invWip != null)
                //{
                //    invWip.Qty = invWip.Qty + item.ProdQty;
                //}
                //else
                //{
                //    invWip = deleteWip.Find(delegate(ProdWipCurEntity p) { return p.WoNo == prodInventory.WoNo && p.TechnicItemId == item.TechnicItemId && p.CurWorkgroupNo == item.CurWorkgroupNo && p.Status == "INV"; });
                //    if (invWip != null)
                //    {
                //        deleteWip.Remove(invWip);
                //        invWip.Qty = item.ProdQty;
                //        updateWip.Add(invWip);
                //    }
                //    else
                //    {
                //        invWip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = "INV" });
                //        if (invWip != null)
                //        {
                //            invWip.Qty = invWip.Qty + item.ProdQty;
                //            updateWip.Add(invWip);
                //        }
                //        else
                //        {
                //            invWip = wip;
                //            invWip.Qty = item.ProdQty;
                //            invWip.Status = "INV";
                //            insWip.Add(invWip);
                //        }
                //    }
                //}
               
                ////修改入库状态的在制品记录
               
                //ProdWipHisEntity wiphis = GetWipInventoryHis(wip, prodInventory.InvNo,"M_INV");
                //insertWipInventoryHis.Add(wiphis);

                //生成入库申请仓库视图 
                InvIappiEntity iappi = CreateInvIappiByWipInventory(iapp.Id, prodInventory.ProdId, item);
                iappis.Add(iappi);
            }
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Update(prodInventory, broker);
                    if (iapp != null) DataAccess.Update(iapp, broker);
                    if (delIAppis.Count > 0) DataAccess.Delete(delIAppis, broker);
                    if (iappis.Count > 0) DataAccess.Insert(iappis, broker);
                    if (delInvItem.Count > 0) DataAccess.Delete(delInvItem, broker);
                    DataAccess.Insert(invItems, broker);
                    //if (deleteWip.Count > 0) DataAccess.Delete(deleteWip, broker);
                    //if (updateWip.Count > 0) DataAccess.Update(updateWip, broker);
                    //if (insWip.Count > 0) DataAccess.Insert(insWip, broker);
                    //if (insertWipInventoryHis.Count > 0) DataAccess.Insert(insertWipInventoryHis, broker);
                   
                    broker.Commit();
                    return prodInventory;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static InvProdInventoryEntity DeleteProdInventory(InvProdInventoryEntity prodInventory)
        {
            if (prodInventory == null) return null;

            //产生入库申请单号
            prodInventory.UpdatedDate = DateTime.Now;
            //List<ProdWipCurEntity> deleteWip = new List<ProdWipCurEntity>();
            //List<ProdWipCurEntity> updateWip = new List<ProdWipCurEntity>();
            //List<ProdWipCurEntity> insWip = new List<ProdWipCurEntity>();
            //List<ProdWipHisEntity> insertWipInventoryHis = new List<ProdWipHisEntity>();
            InvIappEntity iapp = DataAccess.SelectSingle<InvIappEntity>(new InvIappEntity { RelatedId = prodInventory.InvNo });
            List<InvIappiEntity> delIAppis = DataAccess.Select<InvIappiEntity>(new InvIappiEntity { IoAppId = iapp.Id });
            List<InvProdInventoryItemEntity> delInvItem = DataAccess.Select<InvProdInventoryItemEntity>(new InvProdInventoryItemEntity { InvId = prodInventory.Id });

            ////还原inv状态的在制品信息
            //foreach (InvProdInventoryItemEntity item in delInvItem)
            //{
            //    var wip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = Common.WoStatus.WOS_Finish.ToString() });
            //    var invWip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = prodInventory.WoNo, TechnicItemId = item.TechnicItemId, CurWorkgroupNo = item.CurWorkgroupNo, Status = "INV" });

            //    if (wip != null)
            //    {//存在没有入库的在制品
            //        wip.Qty = wip.Qty + item.ProdQty;
            //        updateWip.Add(wip);
            //    }
            //    else
            //    {//不存在没有入库的在制品
            //        ProdWipCurEntity insFinishedWips = invWip;
            //        insFinishedWips.Id = Guid.NewGuid().ToString();
            //        insFinishedWips.Status = Common.WoStatus.WOS_Finish.ToString();
            //        insFinishedWips.Qty = item.ProdQty;
            //        insWip.Add(insFinishedWips);
            //    }

            //    if (invWip.Qty > item.ProdQty)
            //    {
            //        invWip.Qty = invWip.Qty - item.ProdQty;
            //        updateWip.Add(invWip);
            //    }
            //    else if (invWip.Qty == item.ProdQty)
            //    {
            //        deleteWip.Add(invWip);
            //    }
            //    ProdWipHisEntity wiphis = GetWipInventoryHis(wip, prodInventory.InvNo, "D_INV");
            //    insertWipInventoryHis.Add(wiphis);
            //}

            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Delete(prodInventory, broker);
                    if (delInvItem.Count > 0) DataAccess.Delete(delInvItem, broker);
                    //if (deleteWip.Count > 0) DataAccess.Delete(deleteWip, broker);
                    //if (updateWip.Count > 0) DataAccess.Update(updateWip, broker);
                    //if (insWip.Count > 0) DataAccess.Insert(insWip, broker);
                    //if (insertWipInventoryHis.Count > 0) DataAccess.Insert(insertWipInventoryHis, broker);
                    if (iapp != null) DataAccess.Delete(iapp, broker);
                    if (delIAppis.Count > 0) DataAccess.Delete(delIAppis, broker);
                    broker.Commit();
                    return prodInventory;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static InvProdInventoryEntity GetProdInventory(InvProdInventoryEntity prodInventory)
        {
            if (prodInventory == null) return null;
            InvProdInventoryEntity prodInv =new InvProdInventoryEntity();
            if(!string.IsNullOrEmpty(prodInventory.WoNo))
                prodInv.WoNo=prodInventory.WoNo;
            if (!string.IsNullOrEmpty(prodInventory.Id))
                prodInv.Id = prodInventory.Id;
            if (!string.IsNullOrEmpty(prodInventory.InvNo))
                prodInv.InvNo = prodInventory.InvNo;
            return DataAccess.SelectSingle<InvProdInventoryEntity>(prodInv);
    
        }

        public static List<InvProdInventoryEntity> GetProdInventoryList(InvProdInventoryEntity prodInventory)
        {
            if (prodInventory == null) return null;
            InvProdInventoryEntity prodInv = new InvProdInventoryEntity();
            if (!string.IsNullOrEmpty(prodInventory.WoNo))
                prodInv.WoNo = prodInventory.WoNo;
            if (!string.IsNullOrEmpty(prodInventory.ProdId))
                prodInv.ProdId = prodInventory.ProdId;
            if (!string.IsNullOrEmpty(prodInventory.ProdCode))
                prodInv.ProdCode = prodInventory.ProdCode;
            if(!string.IsNullOrEmpty(prodInventory.InvNo))
                prodInv.InvNo=prodInventory.InvNo;
            return DataAccess.Select<InvProdInventoryEntity>(prodInv);

        }


        public static List<InvProdInventoryItemEntity> GetProdInventoryItems(InvProdInventoryEntity prodInventory)
        {
            if (prodInventory == null) return null;
            if (!string.IsNullOrEmpty(prodInventory.InvNo))
                return DataAccess.Select<InvProdInventoryItemEntity>(new InvProdInventoryItemEntity { InvNo = prodInventory.InvNo });
            if(!string.IsNullOrEmpty(prodInventory.Id))
                return DataAccess.Select<InvProdInventoryItemEntity>(new InvProdInventoryItemEntity { InvId = prodInventory.Id});
            return null;
        }

        //计算工单用料可以入库的最大数量
        public static decimal GetAllowInventoryMaxQty(string woNo, string transNo, string technicItemId,string curWorkgroupNo)
        {

            ProdWipCurEntity wip = DataAccess.SelectSingle<ProdWipCurEntity>(new ProdWipCurEntity { WoNo = woNo, TechnicItemId = technicItemId, CurWorkgroupNo = curWorkgroupNo, Status = Common.ProdStatus.PS_FINISH.ToString() });
            if (wip == null) return 0;
            decimal allowReqQty = wip.Qty;
            //已申请未发料的数量
            string sql = @" select isnull(SUM( item.PROD_QTY),0) TotalReqedQty from INV_PROD_INVENTORY trans left join INV_PROD_INVENTORY_ITEM item on trans.INV_NO=item.INV_NO
                            where trans.WO_NO='{0}' and trans.STATUS in('{1}','{2}','{3}','{4}') and item.TECHNIC_ITEM_ID='{5}' and item.CUR_WORKGROUP_NO='{6}' ";
            if (!string.IsNullOrEmpty(transNo))
                sql += " and trans.INV_NO !='" + transNo + "'";

            sql = string.Format(sql, woNo, Common.WoTransStatus.Active.ToString(), Common.WoTransStatus.Run.ToString(), Common.WoTransStatus.Sign.ToString(),
                                Common.WoTransStatus.Daft.ToString(), technicItemId, curWorkgroupNo);
            DataSet ds = DataAccess.SelectDataSet(sql);
            decimal totalReqedQty = 0;
            if (ds != null && ds.Tables.Count > 0)
            {
                totalReqedQty = Convert.ToDecimal(ds.Tables[0].Rows[0][0]);
            }
            return allowReqQty - totalReqedQty;

        }


    }


}	


